package com.company.cloud.base.modules.mgr.controller;

import com.company.cloud.common.auth.annotation.StaffLogc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.base.modules.base.entity.BizTypeInfo;
import com.company.cloud.base.modules.base.service.BizTypeInfoService;
import com.company.cloud.base.modules.mgr.controller.req.BizTypeMgrQueryParam;
import com.company.cloud.base.modules.mgr.controller.req.BizTypeSaveParam;
import com.company.cloud.base.modules.mgr.controller.req.BizTypeUpdateParam;
import com.company.cloud.base.modules.mgr.controller.resp.BizTypeMgrModel;
import com.company.cloud.base.modules.mgr.service.BizTypeCodeMgrService;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 业务编码分类表  Controller
 * </p>
 *
 * @author limengjie
 * @since 2020-05-25
 */
@Authc
@RestController
@RequestMapping("/mgr/base/bizType")
@Api(tags = "Mgr_Base_业务编码分类")
public class BizTypeMgrController {

    @Autowired
    private BizTypeInfoService bizTypeInfoService;

    @Autowired
    private BizTypeCodeMgrService bizTypeCodeMgrService;

    @PostMapping("/query")
    @ApiOperation(value = "列表查询", response = BizTypeMgrModel.class)
    public BaseResult query(@RequestBody BizTypeMgrQueryParam params) {
        IPage<BizTypeInfo> data = bizTypeInfoService.page(new Page<>(params.getPage(), params.getRows()),
                new QueryWrapper<BizTypeInfo>()
                        .like(StringUtils.hasText(params.getTypeName()), "type_name", params.getTypeName())
                        .eq(StringUtils.hasText(params.getShowPlatform()), "show_platform", params.getShowPlatform())
        );
        IPage<BizTypeMgrModel> page = data.convert(companyInfo -> {
            BizTypeMgrModel model = new BizTypeMgrModel();
            BeanUtil.copyProperties(companyInfo, model);
            return model;
        });
        return BaseResult.ok(page);
    }

    @GetMapping("/get")
    @ApiOperation(value = "详情", response = BizTypeMgrModel.class)
    public BaseResult get(@RequestParam("typeCode") String typeCode) {
        BizTypeInfo result = bizTypeInfoService.getById(typeCode);
        if (result == null){
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        BizTypeMgrModel model = new BizTypeMgrModel();
        BeanUtil.copyProperties(result, model);
        return BaseResult.ok(model);
    }

    @PostMapping("/save")
    @StaffLogc(title = "Mgr_Base_业务编码分类新增", action = "Mgr_Base_业务编码分类新增", actionAdo = "Mgr_Base_业务编码分类列表")
    @ApiOperation(value = "新增", response = BaseResult.class)
    public BaseResult save(@RequestBody BizTypeSaveParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        bizTypeCodeMgrService.save(params, userInfo);
        return BaseResult.ok();
    }

    @PostMapping("/update")
    @StaffLogc(title = "Mgr_Base_业务编码分类修改", action = "Mgr_Base_业务编码分类修改", actionAdo = "Mgr_Base_业务编码分类列表")
    @ApiOperation(value = "修改", response = BaseResult.class)
    public BaseResult update(@RequestBody BizTypeUpdateParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        bizTypeCodeMgrService.update(params, userInfo);
        return BaseResult.ok();
    }
}
